package com.dragon.client.controller.supplies;

import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.supplies.IProductCategoryService;
import com.dragon.repository.domain.dto.ProductCategoryDTO;
import com.dragon.repository.domain.vo.ProductCategoryVO;
import com.dragon.repository.entity.ProductCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类别表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-09-11
 */
@RestController
@RequestMapping("productCategory")
@Api(tags = "物资类别")
public class ProductCategoryController {

    @Autowired
    IProductCategoryService service;

    @GetMapping( "list")
    @ApiOperation(value = "类别列表")
    public ResponseData<List<ProductCategoryDTO>> list(@ApiParam(name = "search", value = "搜索字段", required = false,type = "string")
                                                       @RequestParam(required = false) String search){
        List<ProductCategory> list = service.categoryList(search);
        return ResponseData.success(ProductCategoryDTO.convertList(list));
    }

    @PostMapping("save")
    @ApiOperation(value = "添加类别")
    public ResponseData<ProductCategoryDTO> saveCategory(@RequestBody ProductCategoryVO vo){
        ProductCategory category = service.saveCategory(ProductCategoryVO.convert(vo));
        return ResponseData.success(ProductCategoryDTO.convert(category));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateCategory(@RequestBody ProductCategoryVO vo){
        service.updateCategory(ProductCategoryVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteCategory(Integer id){
        service.deleteCategory(id);
        return ResponseData.success();
    }

}
